import math

def direction(x, y):
	"""
	Return the direction component of a vector (in radians), given
	cartesian coordinates.
	"""

	if x==0 and y==0: return 0
	
	if x > 0:
		if y >= 0:
			return math.atan(y / x)
		else:
			return math.atan(y / x)+2*math.pi
	elif x == 0:
		if y > 0:
			return math.pi/2.0
		elif y == 0:
			return 0
		else:
			return math.pi*1.5
	else:
		return math.atan(y / x) + math.pi

def vlen(v):
	return math.sqrt(v[0]*v[0]+v[1]*v[1])

def scale(v,s):
	return tuple(map(lambda x: x*s, v))

def normal(v):
	l=vlen(v)
	return v[0]/l, v[1]/l

def dist(v1,v2):
	v=(v1[0]-v2[0], v1[1]-v2[1])
	return vlen(v)

def rotate(v, r):
	"""
	Rotate vector v r radians around the origin
	"""

	x = v[0]*math.cos(r) - v[1]*math.sin(r)
	y = v[0]*math.sin(r) + v[1]*math.cos(r)

	return x,y
